package com.singfan.script.importdata;

import com.singfan.common.util.EncryptionUtil;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.IBarberDao;
import com.singfan.core.dao.database.IBarberHairServiceDao;
import com.singfan.core.dao.database.IShopHairDao;
import com.singfan.core.dao.mongo.IBarberLocationDao;
import com.singfan.core.dao.database.model.Barber;
import com.singfan.core.dao.database.model.BarberHairService;
import com.singfan.core.dao.database.model.constant.BusinessStatusType;
import com.singfan.core.dao.database.model.constant.PhoneType;
import com.singfan.core.dao.mongo.IShopLocationDao;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Author: hansonghe
 * Date: 2015-08-20 09:47
 * Desc:
 */
@Component
public class BarberSheetParser extends SheetParser {
    @Autowired
    private IBarberDao barberDao;
    @Autowired
    private IBarberHairServiceDao barberHairServiceDao;
    @Autowired
    private IBarberLocationDao barberLocationDao;
    @Autowired
    private IShopHairDao shopHairDao;
    @Autowired
    private IShopLocationDao shopLocationDao;

    @Override
    protected void insertData(XSSFRow row) {
        String defaultPassword = "123456";
        Date now = new Date();

        int colIndex = 0;

        List<BarberHairService> barberHairServiceList = new ArrayList<>();
        Barber barber = new Barber();
        barber.setId((long) row.getCell(colIndex++).getNumericCellValue());
        barber.setNickName(row.getCell(colIndex++).getStringCellValue());
        barber.setRealName(barber.getNickName());
        barber.setGender((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setPositionId((int) row.getCell(colIndex++).getNumericCellValue());
        long pNumber = (long)row.getCell(colIndex++).getNumericCellValue();
        barber.setPhoneNumber(String.valueOf(pNumber));
        barber.setCityId((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setDistrictId((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setBusinessCircleId((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setShopId((long) row.getCell(colIndex++).getNumericCellValue());
        barber.setMasterShopName(row.getCell(colIndex++).getStringCellValue());
        XSSFCell slaveShopCell = row.getCell(colIndex++);
        if (slaveShopCell != null){
            barber.setSlaveShopName(slaveShopCell.getStringCellValue());
        }
        barber.setAddress(row.getCell(colIndex++).getStringCellValue());
        barber.setLongitude(new BigDecimal(row.getCell(colIndex++).getNumericCellValue()));
        barber.setLatitude(new BigDecimal(row.getCell(colIndex++).getNumericCellValue()));
        barber.setAvatar(row.getCell(colIndex++).getStringCellValue());
        barber.setHonor((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setOrderCount((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setFavorableCommentCount((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setCommentCount((int) row.getCell(colIndex++).getNumericCellValue());
        barber.setHairCutOriginalPrice((int) row.getCell(colIndex++).getNumericCellValue() * 100);
        barber.setHairCutRealPrice((int) row.getCell(colIndex++).getNumericCellValue() * 100);
        barber.setHairModelingOriginalPrice((int) row.getCell(colIndex++).getNumericCellValue() * 100);
        barber.setHairModelingRealPrice((int) row.getCell(colIndex++).getNumericCellValue() * 100);
        String barberHairServiceIds = row.getCell(colIndex++).getStringCellValue();
        String[] hairServiceIdArr = barberHairServiceIds.split(";");
        List<Integer> serviceValueList = new ArrayList<>(hairServiceIdArr.length);
        for (String hairService : hairServiceIdArr){
            if (!StringUtil.isNullOrEmpty(hairService)){
                if (hairService.equals("21")){
                    barber.setHairCut(true);
                }
                else if(hairService.equals("22")){
                    barber.setHairPerm(true);
                }
                else if(hairService.equals("23")){
                    barber.setHairDye(true);
                }
                else if(hairService.equals("24")){
                    barber.setHairNurse(true);
                }
                else if(hairService.equals("25")){
                    barber.setHairModeling(true);
                }

                serviceValueList.add(Integer.valueOf(hairService) - 20);
            }
        }

        barber.setCommentScore(barber.getCommentCount() * barber.getHonor());
        barber.setStatus(BusinessStatusType.VERIFY_PASSED);
        barber.setCreateTime(now);

        barber.setPhoneType(PhoneType.UNKNOWN);
        String passwordSalt = UUID.randomUUID().toString().substring(0, 7);
        String password = EncryptionUtil.md5(defaultPassword + "-" + passwordSalt);
        barber.setPassword(password);
        barber.setPasswordSalt(passwordSalt);
        barber.setIdCardNumber("");

        barberDao.create(barber);

        shopHairDao.increaseBarberCount(barber.getShopId());

        if (barber.getHairCutRealPrice() >= 3000 && barber.getHairCutRealPrice() < 6000){
            serviceValueList.add(6);
            shopHairDao.supportServiceType(barber.getShopId(), 6);
            shopLocationDao.updateShopServices(barber.getShopId(), 6);
        }
        else if (barber.getHairCutRealPrice() >= 6000 && barber.getHairCutRealPrice() < 9000){
            serviceValueList.add(7);
            shopHairDao.supportServiceType(barber.getShopId(), 7);
            shopLocationDao.updateShopServices(barber.getShopId(), 7);
        }
        else if (barber.getHairCutRealPrice() >= 9000){
            serviceValueList.add(8);
            shopHairDao.supportServiceType(barber.getShopId(), 8);
            shopLocationDao.updateShopServices(barber.getShopId(), 8);
        }

        barberLocationDao.createBarberLocation(barber.getId(), barber.getCityId(), barber.getBusinessCircleId(), serviceValueList, barber.getLongitude().doubleValue(), barber.getLatitude().doubleValue(), barber.getPositionId());

        for (BarberHairService serviceItem : barberHairServiceList){
            barberHairServiceDao.create(serviceItem);
        }
    }
}
